<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <!-- <el-form-item label="收费站" prop="deptId">
        <el-tree-select v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
          class="margin-right" placeholder="请选择部门" check-strictly clearable @change="bindChangeTree($event,'deptId')" />

      </el-form-item> -->
      <el-form-item label="落实类型" prop="implType">
        <el-select v-model="queryParams.implType" placeholder="请选择落实类型" clearable>
          <el-option v-for="dict in coo_impl_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="证明人" prop="certifier">
        <el-input v-model="queryParams.certifier" placeholder="请输入证明人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['coordination:implement:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="implementList" @selection-change="handleSelectionChange" show-overflow-tooltip>
      <el-table-column type="index" width="65" label="序号" align="center" />
      <el-table-column label="收费站" align="center" prop="deptName" />
      <el-table-column label="下发时间" align="center" prop="sendTime" />
      <el-table-column label="公文名称" align="center" prop="name">
        <template #default="scope">
          <el-button link type="primary" @click.stop="previewFun(scope.row.fileUrl,scope.row.id)" v-if="scope.row.fileUrl">{{scope.row.name}}</el-button>
        </template> </el-table-column>
      <el-table-column label="公文来源" align="center" prop="source">
        <template #default="scope">
          <dict-tag :options="coo_official_source" :value="scope.row.source" />
        </template>
      </el-table-column>
      <el-table-column label="要求" align="center" prop="demand" />

      <el-table-column label="落实类型" align="center" prop="implType">
        <template #default="scope">
          <dict-tag :options="coo_impl_type" :value="scope.row.implType" />
        </template>
      </el-table-column>
      <el-table-column label="印证资料" align="center" prop="proof">
        <template #default="scope">
          <el-button link type="primary" @click.stop="previewFun(scope.row.proof)" v-if="scope.row.proof">查看</el-button> </template>
      </el-table-column>
      <el-table-column label="证明人" align="center" prop="certifierName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['coordination:implement:edit']">落实情况上报</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改公文落实情况上报对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="implementRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="落实类型" prop="implType">
              <el-select style="width:100%" v-model="form.implType" placeholder="请选择落实类型" @change="bindChangeSelect">
                <el-option v-for="dict in coo_impl_type" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item style="width:100%" label-width="120" label="参会人员数" prop="attendeeNo" v-if="selectType==1">
              <el-input v-model="form.attendeeNo" type="input" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item label-width="120" label="学习人员数" prop="attendeeNo" v-if="selectType==2">
              <el-input style="width:100%" v-model="form.attendeeNo" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item label-width="120" label="领导姓名" prop="leader" v-if="selectType==3">
              <el-select style="width:100%" v-model="form.leader" placeholder="请选择">
                <el-option v-for="(item,index) in userList" :key="item" :label="item.userName" :value="item.userId"></el-option>
              </el-select>
            </el-form-item>
          </el-col><el-col :span="12">
            <el-form-item label="证明人" prop="certifier">
              <el-select style="width:100%" v-model="form.certifier" placeholder="请选择">
                <el-option v-for="(item,index) in userList" :key="item" :label="item.userName" :value="item.userId"></el-option>
              </el-select>
            </el-form-item>
          </el-col><el-col :span="12">
            <el-form-item label="印证资料" prop="proof">
              <FileUpload :modelValue="form.proof" v-model="form.proof" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="落实描述" prop="implDescribe">
              <el-input v-model="form.implDescribe" type="textarea" placeholder="请输入内容" :rows="6" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="总结" prop="summary">
              <el-input v-model="form.summary" type="textarea" placeholder="请输入内容" :rows="6"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <Viewer ref="fileViewerRef" :dialog-doc="dialogDoc" />
  </div>
</template>

<script setup name="Implement">
import { listImplement, getImplement, delImplement, addImplement, updateImplement, getImplementRead } from "@/api/coordination/implement";
import {
  getCAuditorUser
} from "@/api/charge/cAuditor";
import { getCKInfo } from '@/utils/auth'
const { proxy } = getCurrentInstance();
const { coo_impl_type, coo_official_source } = proxy.useDict('coo_impl_type', 'coo_official_source');
import Viewer from "@/components/Custom/viewer.vue";

const baseUrl = import.meta.env.VITE_APP_BASE_API;

const implementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref([])
const selectType = ref(1)
const deptId = ref(Number(getCKInfo('deptId')))
const userList = ref([])
const variableInfoList = ref([])
const dialogDoc = ref(false)
const fileViewerRef = ref()
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    officeId: null,
    deptId: null,
    implType: null,
    implDescribe: null,
    proof: null,
    certifier: null,
    status: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公文落实情况上报列表 */
function getList() {
  loading.value = true;
  listImplement(queryParams.value).then(response => {
    implementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

const getUserList = () => {
  getCAuditorUser({ deptId: deptId.value }).then(res => {
    userList.value = res.rows
      ;
  })
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    officeId: null,
    deptId: null,
    implType: null,
    implDescribe: null,
    proof: null,
    certifier: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("implementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加公文落实情况上报";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getImplement(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改公文落实情况上报";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["implementRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
    
        updateImplement(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          if(form.value.status!=1){postRead(form.value.id)}
          getList();
        });
      } else {
        addImplement(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除公文落实情况上报编号为"' + _ids + '"的数据项？').then(function () {
    return delImplement(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('coordination/implement/export', {
    ...queryParams.value
  }, `公文管理_${new Date().getTime()}.xlsx`)
}

const bindChangeSelect = (val) => {
  selectType.value = val;
}
const postRead = (id) => {
  getImplementRead({ id }).then(response => {

  });
}
// 预览
const previewFun = (file, id) => {
  // 发送已读
  if(id){postRead(id)}
  
  dialogDoc.value = true;
  let data = {};
  // 上传过的文件组成完成的网络路径
  if (file&&file.indexOf(',')==-1) {
    data.src = `${baseUrl}${file}`;
  }else if(file&&file.indexOf(',')!=-1){
    let src=file.split(',');
    let srcArr=[]
    src.forEach(item=>{srcArr.push(`${baseUrl}${item}`)})
    data.src = srcArr.join(',');
    file=srcArr[0]
  }

  const arr = file.split(".");
  const suffix = arr[arr.length - 1];
  if (suffix == "docx" || suffix == "doc") {
    fileViewerRef.value?.viewDocx(data, '公文');
  } else if (suffix == "xlsx" || suffix == "xls") {
    fileViewerRef.value?.viewXlsx(data);
  } else if (suffix == "jpg" || suffix == "jpeg" || suffix == "png") {
    fileViewerRef.value?.viewImg(data);
  } else if (suffix == "pdf") {
    fileViewerRef.value?.viewPdf(data);
  }
};
getUserList()
getList();
</script>
